<?php

class RunTimeWorkflowNodeFactory
{
  
  /**
   * @var RunTimeWorkflowNodeFactory
   */
  private static $instance = null;

  private function __construct ()
  {
  
  }

  /**
   * @return RunTimeWorkflowNodeFactory
   */
  public static function getInstance ()
  {
    if (is_null(self::$instance))
    {
      self::$instance = new self();
    }
    return self::$instance;
  }

  // TODO phpdoc
  public function create ($param1)
  {
    if (is_numeric($param1))
    {
      return $this->__createFromWorkItemId($param1);
    }
    elseif ($param1 instanceof wfWorkflowWorkItem)
    {
      return $this->__createFromWorkItem($param1);
    }
    else
    {
      throw new InvalidArgumentException(); // TODO specify exception
    }
  }

  /**
   * @param integer $id
   * @return AbstractRunTimeWorkflowNode
   */
  protected function __createFromWorkItemId ($id)
  {
    $work_item = wfWorkflowWorkItemTable::getInstance()->find(array(
      'id' => $id));
    return $this->__createFromWorkItem($work_item);
  }

  /**
   * @param wfWorkflowWorkItem $item
   * @return AbstractRunTimeWorkflowNode
   */
  protected function __createFromWorkItem (wfWorkflowWorkItem $item)
  {
    $class = $item->getClass();
    return new $class($item);
  }
}